package com.example.exception;

import com.example.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//捕获所有controller中的异常
@ControllerAdvice("com.example.controller")
public class GlobalExceptionHandler {

    //捕获父类exception
    @ExceptionHandler(Exception.class)
    //表明返回的是json格式
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.error();
    }

    //捕获自定义类customexception类
    @ExceptionHandler(CustomException.class)
    //表明返回的是json格式
    @ResponseBody
    public Result error(CustomException e){
        return Result.error(e.getCode(),e.getMsg());
    }
}
